package com.jeff.permission.demo;

import android.Manifest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.jeff.permission.CheckPermission;
import com.jeff.permission.PermissionItem;
import com.jeff.permission.PermissionListener;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button bt_camera_permission;
    private Button bt_permissions;
    private Button bt_go_setting;
    private Button bt_system_setting;
    private Button bt_system_alert;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bt_camera_permission = findViewById(R.id.bt_camera_permission);
        bt_camera_permission.setOnClickListener(this);
        bt_permissions = findViewById(R.id.bt_permissions);
        bt_permissions.setOnClickListener(this);
        bt_go_setting = findViewById(R.id.bt_go_setting);
        bt_go_setting.setOnClickListener(this);

        bt_system_setting = findViewById(R.id.bt_system_setting);
        bt_system_setting.setOnClickListener(this);

        bt_system_alert = findViewById(R.id.bt_system_alert);
        bt_system_alert.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_camera_permission:
                CheckPermission.instance(this)
                        .check(new PermissionItem(Manifest.permission.CAMERA).rationalMessage("需要使用相机权限").rationalButton("获取"), new PermissionListener() {
                            @Override
                            public void permissionGranted() {
                                toast("Permission Granted");
                            }

                            @Override
                            public void permissionDenied() {
                                toast("Permission Denied");
                            }
                        });
                break;

            case R.id.bt_permissions:
                CheckPermission.instance(this)
                        .check(new PermissionItem(Manifest.permission.READ_PHONE_STATE, Manifest.permission.SEND_SMS, Manifest.permission.RECORD_AUDIO, Manifest.permission.READ_CALENDAR),
                                new PermissionListener() {
                                    @Override
                                    public void permissionGranted() {
                                        toast("Permission Granted");
                                    }

                                    @Override
                                    public void permissionDenied() {
                                        toast("Permission Denied");
                                    }
                                });
                break;


            case R.id.bt_go_setting:
                CheckPermission.instance(this)
                        .check(new PermissionItem(Manifest.permission.READ_EXTERNAL_STORAGE).needGotoSetting(true), new PermissionListener() {
                            @Override
                            public void permissionGranted() {
                                toast("Permission Granted");
                            }

                            @Override
                            public void permissionDenied() {
                                toast("Permission Denied");
                            }
                        });
                break;

            case R.id.bt_system_setting:
                CheckPermission.instance(this)
                        .check(new PermissionItem(Manifest.permission.WRITE_SETTINGS), new PermissionListener() {
                            @Override
                            public void permissionGranted() {
                                toast("Permission Granted");
                            }

                            @Override
                            public void permissionDenied() {
                                toast("Permission Denied");
                            }
                        });
                break;

            case R.id.bt_system_alert:
                CheckPermission.instance(this)
                        .check(new PermissionItem(Manifest.permission.SYSTEM_ALERT_WINDOW), new PermissionListener() {
                            @Override
                            public void permissionGranted() {
                                toast("Permission Granted");
                            }

                            @Override
                            public void permissionDenied() {
                                toast("Permission Denied");
                            }
                        });
                break;
        }
    }

    private void toast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

}
